﻿// This file is part of VideoLan.Inteop
//
// Copyright (C) 2008, Michael Melendez, http://www.MeediOS.com
//
//VideoLan.Inteop is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either 
//version 2.1 of the License, or (at your option) any later version.
//
//VideoLan.Inteop is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public 
//License along with this library.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Collections.Generic;
using System.Text;

namespace VideoLan
{
    /// <summary>
    /// </summary>
    /// <remarks></remarks>
    /// <example></example>
    public class VlcMedia : IDisposable
    {
        #region Private Members

        /// <summary>
        /// COM pointer to a vlc exception.  We will only use 1 exception pointer, 
        /// so we must always clear it out after use
        /// </summary>
        private libvlc_exception_t p_exception;

        /// <summary>
        /// COM pointer to our vlc media instance
        /// </summary>
        internal IntPtr p_media;

        #endregion

        #region Properties

        #region public string MRL
        /// <summary>
        /// Gets the MRL of the VlcMedia
        /// </summary>
        /// <value></value>
        public string MRL
        {
            get
            {
                string rtn = InteropMethods.libvlc_media_get_mrl(p_media, ref p_exception);
                p_exception.CheckException();
                return rtn;
            }
        }
        #endregion
        #region public VlcState State
        /// <summary>
        /// 
        /// Gets the State of the VlcMedia
        /// </summary>
        /// <value></value>
        public VlcState State
        {
            get
            {
                VlcState rtn = InteropMethods.libvlc_media_get_state(p_media, ref p_exception);
                p_exception.CheckException();
                return rtn;
            }
        }
        #endregion
        #region public long Length
        /// <summary>
        /// Gets the Length of the VlcMedia
        /// </summary>
        /// <value></value>
        public long Length
        {
            get
            {
                long rtn = InteropMethods.libvlc_media_get_duration(p_media, ref p_exception);
                p_exception.CheckException();
                return rtn;
            }
        }
        #endregion
        #region public bool IsPreparsed
        /// <summary>
        /// Gets the IsPreparsed of the VlcMedia
        /// </summary>
        /// <value></value>
        public bool IsPreparsed
        {
            get
            {
                bool rtn = InteropMethods.libvlc_media_is_preparsed(p_media, ref p_exception);
                p_exception.CheckException();
                return rtn;
            }
        }
        #endregion

        #endregion

        #region Constructors/Destructors

        #region internal VlcMedia(IntPtr p_media)
        /// <summary>
        /// Initializes a new instance of the <b>VlcMedia</b> class.
        /// </summary>
        /// <param name="p_media"></param>
        internal VlcMedia(IntPtr p_media) : this(p_media, false){}
        #endregion

		#region internal VlcMedia(IntPtr p_media, bool retain)
        /// <summary>
        /// Initializes a new instance of the <b>VlcMedia</b> class.
        /// </summary>
        /// <param name="p_media"></param>
        /// <param name="retain"></param>
        internal VlcMedia(IntPtr p_media, bool retain)
        {
            //set our Media instance pointer
            this.p_media = p_media;
            //Initalize our exception pointer
            p_exception = new libvlc_exception_t();
            p_exception.Initalize();

            if(retain && p_media != IntPtr.Zero)
            {
                InteropMethods.libvlc_media_retain(p_media);
            }
        }
		#endregion

        #region ~VlcMedia()
        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before 
        /// the <b>VlcMedia</b> is reclaimed by garbage collection.
        /// </summary>
        ~VlcMedia()
        {
            Dispose(false);
        }
        #endregion

        #endregion

        #region IDisposable Methods

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            // release managed code
            if (disposing)  
            {

            }

            //release unmanaged code
            if (p_media != IntPtr.Zero)
                InteropMethods.libvlc_media_release(p_media);
            p_media = IntPtr.Zero;
        }

        #endregion

        public void AddOption(byte[] option)
        {
            
            InteropMethods.libvlc_media_add_option(p_media,option, ref p_exception);
            p_exception.CheckException();
        }

        public string ReadMeta(libvlc_meta_t tag)
        {
            string rtn = InteropMethods.libvlc_media_get_meta(p_media, tag, ref p_exception);
            p_exception.CheckException();
            return rtn;
            
        }
    }
}
